package com.gnerv.sylvanas.business.manage.logic;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.framework.core.BaseUkIdGenerate;
import com.gnerv.sylvanas.business.manage.model.bo.DictBO;
import com.gnerv.sylvanas.business.manage.model.dto.DictCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.DictUpdateDTO;
import com.gnerv.sylvanas.business.manage.model.vo.DictIndexVO;
import com.gnerv.sylvanas.business.manage.model.vo.DictDetailsVO;
import com.gnerv.sylvanas.business.manage.model.vo.DictVO;
import com.gnerv.sylvanas.business.manage.operation.DictItemOperationService;
import com.gnerv.sylvanas.business.manage.operation.DictOperationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class DictLogicService  {

    private final DictOperationService dictOperationService;
    private final DictItemOperationService dictItemOperationService;
    private final BaseUkIdGenerate baseUkIdGenerate;

    public RestResult<DictIndexVO> index(String name, int page, int size) {
        List<DictVO> dictList = dictOperationService.selectListVOByName(name, page, size);
        int count = dictOperationService.selectCountByName(name);
        Map<String, DictDetailsVO> dictDetailsVOMap = dictOperationService.selectMapVOByCode(List.of("data_status"));
        DictIndexVO dictIndexVO = new DictIndexVO();
        dictIndexVO.setDictList(dictList);
        dictIndexVO.setDictDetailsMap(dictDetailsVOMap);
        return RestResult.ok(dictIndexVO).page(page, size, count);
    }
    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(DictCreateDTO dictCreateDTO) {
        DictBO dictBO = dictCreateDTO.toDictBO();
        dictBO.setUkId(baseUkIdGenerate.generate());
        dictOperationService.create(dictBO);
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        dictOperationService.deleteByUkId(ukId);
        dictItemOperationService.deleteByDictUkId(ukId);
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(DictUpdateDTO dictUpdateDTO) {
        dictOperationService.updateByUkId(dictUpdateDTO.toDictBO());
        return RestResult.ok();
    }

    
    @Transactional(readOnly = true)
    public RestResult<DictVO> selectVOByUkId(String ukId) {
        return RestResult.ok(dictOperationService.selectVOByUkId(ukId));
    }

    
    public RestResult<DictDetailsVO> selectDetailsVOByUkId(String ukId) {
        return RestResult.ok(dictOperationService.selectDetailsVOByUkId(ukId));
    }

    
    public RestResult<DictDetailsVO> selectDetailsVOByCode(String code) {
        return RestResult.ok(dictOperationService.selectDetailsVOByCode(code));
    }

    @Transactional(readOnly = true)
    public RestResult<List<DictVO>> selectListByName(String name) {
        return RestResult.ok(dictOperationService.selectListByName(name));
    }

    
    public RestResult<List<DictDetailsVO>> selectListDetailsVOByName(String name) {
        return RestResult.ok(dictOperationService.selectListDetailsVOByName(name));
    }

    public RestResult<List<DictVO>> selectListVOByName(String name, int page, int size) {
        List<DictVO> dictList = dictOperationService.selectListVOByName(name, page, size);
        int count = dictOperationService.selectCountByName(name);
        return RestResult.ok(dictList).page(page, size, count);
    }
}
